﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;

    public class OrderSequenceRuleDetail : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnCancel;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlOppOrderStrategy;
        protected DropDownList ddlOrderTypeInclude;
        protected DropDownList ddlPriorityFrom;
        protected DropDownList ddlPriorityTo;
        protected HtmlForm F79633;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblAllocateRuleDetail;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblOppOrderRuleID;
        protected Label lblOppOrderStrategy;
        protected Label lblOrderDateEndDays;
        protected Label lblOrderDateStartDays;
        protected Label lblOrderTypeInclude;
        protected Label lblPriorityFrom;
        protected Label lblPriorityTo;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtLineID;
        protected TextBox txtOppOrderRuleID;
        protected TextBox txtOrderDateEndDays;
        protected TextBox txtOrderDateStartDays;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("OrderSequenceRule.aspx?from={0}&detail={1}&ID={2}", this.hdnFrom.Value, "1", base.Server.UrlEncode(this.txtOppOrderRuleID.Text.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOppOrderRuleID.Text.Trim()))
            {
                this.lblMsg.Text = "订单顺序规则不存在!";
            }
            else
            {
                bool success = false;
                try
                {
                    int tempInt = 0;
                    OppOrderRuleDetailInfo oppOrderRuleDetailInfo = new OppOrderRuleDetailInfo {
                        LineID = int.Parse(this.txtLineID.Text.Trim()),
                        OppOrderRuleID = this.txtOppOrderRuleID.Text.Trim(),
                        OppOrderStrategy = this.ddlOppOrderStrategy.SelectedValue
                    };
                    if (int.TryParse(this.txtOrderDateStartDays.Text.Trim(), out tempInt))
                    {
                        oppOrderRuleDetailInfo.ReqShipDateStartDays = int.Parse(this.txtOrderDateStartDays.Text.Trim());
                    }
                    else
                    {
                        oppOrderRuleDetailInfo.ReqShipDateStartDays = tempInt;
                    }
                    if (int.TryParse(this.txtOrderDateEndDays.Text.Trim(), out tempInt))
                    {
                        oppOrderRuleDetailInfo.ReqShipDateEndDays = int.Parse(this.txtOrderDateEndDays.Text.Trim());
                    }
                    else
                    {
                        oppOrderRuleDetailInfo.ReqShipDateEndDays = tempInt;
                    }
                    oppOrderRuleDetailInfo.OrderTypeInclude = this.ddlOrderTypeInclude.SelectedValue;
                    oppOrderRuleDetailInfo.PriorityFrom = this.ddlPriorityFrom.SelectedValue;
                    oppOrderRuleDetailInfo.PriorityTo = this.ddlPriorityTo.SelectedValue;
                    oppOrderRuleDetailInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    oppOrderRuleDetailInfo.CreatedBy = base.UserInfo.UserID;
                    if (this.hdnType.Value == "edit")
                    {
                        oppOrderRuleDetailInfo.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(CrossdockServiceClientProxy.GetInstance().UpdateOppOrderRuleDetailInfo(oppOrderRuleDetailInfo)))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(CrossdockServiceClientProxy.GetInstance().InsertOppOrderRuleDetailInfo(oppOrderRuleDetailInfo)))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = "保存失败!" + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("OrderSequenceRule.aspx?from={0}&detail={1}&ID={2}", this.hdnFrom.Value, "1", base.Server.UrlEncode(this.txtOppOrderRuleID.Text.Trim())));
                }
            }
        }

        protected void ddlOppOrderStrategy_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.ddlOppOrderStrategy.SelectedIndex)
            {
                case 0:
                    this.txtOrderDateEndDays.Enabled = false;
                    this.txtOrderDateStartDays.Enabled = false;
                    this.ddlOrderTypeInclude.Enabled = false;
                    this.ddlPriorityFrom.Enabled = false;
                    this.ddlPriorityTo.Enabled = false;
                    break;

                case 1:
                    this.txtOrderDateEndDays.Enabled = false;
                    this.txtOrderDateStartDays.Enabled = false;
                    this.ddlOrderTypeInclude.Enabled = false;
                    this.ddlPriorityFrom.Enabled = true;
                    this.ddlPriorityTo.Enabled = true;
                    break;

                case 2:
                    this.txtOrderDateEndDays.Enabled = true;
                    this.txtOrderDateStartDays.Enabled = true;
                    this.ddlOrderTypeInclude.Enabled = false;
                    this.ddlPriorityFrom.Enabled = false;
                    this.ddlPriorityTo.Enabled = false;
                    break;

                case 3:
                    this.txtOrderDateEndDays.Enabled = true;
                    this.txtOrderDateStartDays.Enabled = true;
                    this.ddlOrderTypeInclude.Enabled = false;
                    this.ddlPriorityFrom.Enabled = false;
                    this.ddlPriorityTo.Enabled = false;
                    break;

                case 4:
                    this.txtOrderDateEndDays.Enabled = false;
                    this.txtOrderDateStartDays.Enabled = false;
                    this.ddlOrderTypeInclude.Enabled = true;
                    this.ddlPriorityFrom.Enabled = false;
                    this.ddlPriorityTo.Enabled = false;
                    break;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlPriorityFrom, "OrderPriority ");
            base.BindData2DDLBySysCode(this.ddlPriorityTo, "OrderPriority ");
            base.BindData2DDLBySysCode(this.ddlOrderTypeInclude, "OrderType ");
            base.BindData2DDLBySysCode(this.ddlOppOrderStrategy, "OppOrderStrategy ");
            this.ddlOppOrderStrategy.SelectedIndex = 0;
            this.ddlOppOrderStrategy_SelectedIndexChanged(null, null);
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string oppOrderRuleID = base.Request.QueryString["ID"].ToString();
                this.txtOppOrderRuleID.Text = oppOrderRuleID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string seqNo = base.Request.QueryString["SeqNO"].ToString();
                this.txtLineID.Text = seqNo;
                this.txtLineID.Enabled = false;
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetOppOrderRule2From();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOppOrderRule2From()
        {
            if ((this.txtOppOrderRuleID.Text.Trim() != "") && (this.txtLineID.Text.Trim() != ""))
            {
                OppOrderRuleDetailInfo oppOrderRuleDetailInfo = CrossdockServiceClientProxy.GetInstance().GetOppOrderRuleDetailInfoByID(this.txtOppOrderRuleID.Text.Trim(), this.txtLineID.Text.Trim());
                if (oppOrderRuleDetailInfo != null)
                {
                    this.ddlOrderTypeInclude.SelectedValue = oppOrderRuleDetailInfo.OrderTypeInclude;
                    this.ddlPriorityFrom.SelectedValue = oppOrderRuleDetailInfo.PriorityFrom;
                    this.ddlPriorityTo.SelectedValue = oppOrderRuleDetailInfo.PriorityTo;
                    this.ddlOppOrderStrategy.SelectedValue = oppOrderRuleDetailInfo.OppOrderStrategy;
                    this.chkIsActive.Checked = oppOrderRuleDetailInfo.IsActive == "Y";
                    this.txtOrderDateEndDays.Text = oppOrderRuleDetailInfo.OrderDateEndDays.ToString();
                    this.txtOrderDateStartDays.Text = oppOrderRuleDetailInfo.OrderDateStartDays.ToString();
                    this.txtOppOrderRuleID.Text = oppOrderRuleDetailInfo.OppOrderRuleID;
                    this.txtLineID.Text = oppOrderRuleDetailInfo.LineID.ToString();
                    this.txtOppOrderRuleID.Enabled = false;
                    this.txtLineID.Enabled = false;
                }
            }
        }
    }
}

